Repository navigation
Conversation
With by_column, drop_last_batch was forwarded to the inner map over input rows, so it dropped the last input batch instead of an output batch. Dataset then lost complete groups (the buffered group is only flushed on the last row), and IterableDataset returned a truncated group. Groups are always complete, so drop_last_batch no longer drops anything with by_column, and both dataset types agree. Fixes huggingface#9028
|
One additional boundary I checked on 6c12105: an indexed view whose entire length is below batch_size. With three rows selected in non-physical order, two grouping keys and a ClassLabel feature, the parent returns unbatched scalar rows for Dataset and no rows for IterableDataset when drop_last_batch=True. This branch returns the expected three complete singleton groups, preserving order and label names; False remains a passing control. Four new cases pass (parent: two failures/two controls). Existing batching tests in both modules: 192 passed, 4 skipped, plus one existing torch-construction warning. Windows/Python 3.12, local data; Ruff passes. Codex-assisted reproducer: import pytest
from datasets import Dataset, Features, Value, ClassLabel
@pytest.mark.parametrize("streaming", [False, True])
@pytest.mark.parametrize("drop_last", [False, True])
def test_grouped_short_indexed_view(tmp_path, streaming, drop_last):
features = Features(
{
"id": Value("int64"),
"group": Value("string"),
"label": ClassLabel(names=["left", "right"]),
}
)
ds = Dataset.from_dict(
{
"id": [0, 1, 2, 3, 4, 5],
"group": ["unused", "a", "unused", "a", "unused", "b"],
"label": [0, 0, 0, 1, 0, 1],
},
features=features,
)
ds = ds.select([5, 1, 3])
assert ds._indices is not None
if streaming:
ds = ds.to_iterable_dataset()
batched = ds.batch(
by_column=["group", "label"], batch_size=4, drop_last_batch=drop_last
)
rows = list(batched)
assert [row["id"] for row in rows] == [[5], [1], [3]]
assert [row["label"] for row in rows] == [[1], [0], [1]]
assert batched.features["label"].feature.names == ["left", "right"] |
|
Thanks @JonnyFaeth, that's a useful boundary. On |
|
Gentle ping on this one when you have a moment. The branch merges cleanly with main, and the CI workflows are still waiting for approval since this is my first PR here. Happy to adjust anything if needed. Thanks! |
Fixes #9028
With
by_column,drop_last_batchwas passed to the innermapover input rows, so it dropped the lastbatch_sizeinput rows instead of an output batch.Dataset.batchthen lost complete groups (and every row for a single-group dataset), whileIterableDataset.batchdropped or truncated the trailing group(s) (e.g. it returned[5, 6, 7]for the group[5, 6, 7, 8]).This PR stops forwarding
drop_last_batchin theby_columnbranch of bothDataset.batchandIterableDataset.batch, and notes it in the docstrings. Withby_column, a batch is a whole group andbatch_sizeonly sets the accumulation chunk size (as the docs already say), so no output batch is ever incomplete and there is nothing to drop. Both types now return the same groups with and withoutdrop_last_batch:(
batch_size=4, values are the row ids.)drop_last_batchis still part of theDataset.batchfingerprint, soTrueandFalsewrite separate (identical) cache files.Why this option over the others listed in the issue:
drop_last_batchgenerically, for a combination that can simply be well defined.If you'd prefer one of those instead, I'm happy to switch.
Tests: added
test_dataset_batch_by_column_with_drop_last_batch, parametrized overDatasetandIterableDataset(same pattern astest_dataset_batch_by_column_with_nulls), covering both cases from the issue, a single group larger thanbatch_size, and a length that is an exact multiple ofbatch_size. The new test fails onmainand passes here;pytest tests/test_arrow_dataset.py tests/test_iterable_dataset.py -k batchpasses andmake qualityis clean.